﻿CREATE PROCEDURE [dbo].[Text_Book_Get]
	@userId UNIQUEIDENTIFIER = NULL
AS
BEGIN
	SELECT [BookId]
				,[Author]
				,[CoverUrl]
	FROM [dbo].[text_Book]

	SELECT DISTINCT bt.[BookTreeId]
			,bt.[ParentBookTreeId]
			,bt.[LeftKey]
			--,bt.[RightKey]
			,bt.[Level]
			,bt.[BookId]
			--,bt.[HashTag]
			,bt.[IsHead]
			,bt.[Text]
			,bt.[Published]
			--,bt.[UpdateDate]
				,c.[Url]
	FROM [dbo].[text_BookTree] AS bt
	LEFT OUTER JOIN [dbo].[text_BookTree_Cache] AS c ON bt.[BookTreeId] = c.[BookTreeId]
	WHERE [Level] <= 2 OR ([IsHead] = 1 AND bt.[Level] < (SELECT MAX([Level]) FROM [dbo].[text_BookTree] WHERE [IsHead] = 1 AND [BookId] = bt.[BookId]))
	ORDER BY bt.[BookId]
END